﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyVideoViewerLibrary;
using System.Windows.Threading;

namespace MyVideoViewerManager
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MyVideoViewerManagerBusiness bl;
        private Queue<Series> queue;
        public MainWindow()
        {
            InitializeComponent();
            queue = new Queue<Series>();

            bl = new MyVideoViewerManagerBusiness();
            
            bl.DataUpdated += new UpdateEvent(bl_DataUpdated);
            bl.UpdateSeriesCompleted += new EventHandler(bl_UpdateSeriesCompleted);

            settingsMenu.Click += new RoutedEventHandler(settingsMenu_Click);
            updateSeriesButton.Click += new RoutedEventHandler(updateSeriesButton_Click);
            importFolderButton.Click += new RoutedEventHandler(importFolderButton_Click);

            seriesListBox.SelectionChanged += new SelectionChangedEventHandler(seriesListBox_SelectionChanged);

            seriesControl.StatusUpdated += new StatusUpdatedEvent(seriesControl_StatusUpdated);

            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void bl_UpdateSeriesCompleted(object sender, EventArgs e)
        {
            ProcessQueue();
        }

        void seriesControl_StatusUpdated(string statusText, bool showControl, bool isIndeterminate, int percent)
        {
            statusControl.SetDisplay(showControl);
            statusControl.Update(statusText, isIndeterminate, percent);
        }

        void seriesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            updateSeriesButton.Visibility = Visibility.Collapsed;
            if (seriesListBox.SelectedItems.Count == 1)
            {
                Series series = seriesListBox.SelectedValue as Series;
                seriesControl.Series = series;
            }
            else if (seriesListBox.SelectedItems.Count > 1)
            {
                updateSeriesButton.Visibility = Visibility.Visible;
            }
        }

        void importFolderButton_Click(object sender, RoutedEventArgs e)
        {
            ImportFolderWindow importFolderWindow = new ImportFolderWindow();
            importFolderWindow.ShowDialog();
            LoadSeries();
        }

        void bl_DataUpdated(string updateText)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
            {
                statusControl.Update(updateText);
            });
        }

        void updateSeriesButton_Click(object sender, RoutedEventArgs e)
        {
            statusControl.SetDisplay(true);
            foreach (Series series in seriesListBox.SelectedItems.Cast<Series>())
                queue.Enqueue(series);
            ProcessQueue();
        }

        void ProcessQueue()
        {
            if (queue.Count > 0)
            {
                Series series = queue.Dequeue();
                bl.UpdateSeriesAsync(ref series);
            }
            else
            {
                statusControl.SetDisplay(false);
            }
        }

        void settingsMenu_Click(object sender, RoutedEventArgs e)
        {
            SettingsWindow settingsWindow = new SettingsWindow();
            settingsWindow.ShowDialog();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {            
            if (!MyVideoViewerBusiness.IsValid)
            {
                MessageBox.Show("Please run the configuration utility before continuing.");
                Close();
            }
            if (string.IsNullOrEmpty(MyVideoViewerManager.Properties.Settings.Default.ChosenLanguage))
            {
                settingsMenu_Click(null, null);
            }
            LoadSeries();
        }

        private void LoadSeries()
        {
            seriesListBox.ItemsSource = null;
            seriesListBox.ItemsSource = bl.GetSeries();
        }
    }
}
